home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 176-200 / disk_179 / unixutil / wc.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  3KB  |  148 lines

  1. /* wc.c - count the number of lines, words, and characters in a file.    *
  2.  *                                    *
  3.  * wc [<file> ...]                            *
  4.  *                                    *
  5.  * wc (C) 1988 by Gary L. Brant                     *
  6.  *                                    *
  7.  * :ts=8                                */
  8.  
  9. #include <stdio.h>
  10. #define     FALSE        0
  11. #define     TRUE        1
  12. #define     COUNT_LINES    4
  13. #define     COUNT_WORDS    2
  14. #define     COUNT_CHARS    1
  15. int cur_flag, def_flag = COUNT_LINES | COUNT_WORDS | COUNT_CHARS;
  16. int head;
  17. long  lines, words, chars;
  18. long  tlines=0, twords=0, tchars=0;
  19. char string[] = "         ";
  20.  
  21. main (argc, argv)    /* count lines, words, chars in input */
  22. int  argc;
  23. char *argv[];
  24. {
  25.    register int i = 0, j;
  26.    register char ch;
  27.    FILE *input, *fopen ();
  28.    void fclose ();
  29.  
  30.    cur_flag = def_flag;
  31.    while (++i < argc) {
  32.       if (argv [i][0] == '-') {     /* remember to bump i in loop */
  33.      def_flag = cur_flag;
  34.      cur_flag = 0;
  35.      for (j = 1; (ch = argv[i][j]) != '\0'; j++)
  36.         switch (ch) {
  37.         case 'l':
  38.         cur_flag |= COUNT_LINES;
  39.         break;
  40.         case 'w':
  41.         cur_flag |= COUNT_WORDS;
  42.         break;
  43.         case 'c':
  44.         cur_flag |= COUNT_CHARS;
  45.         break;
  46.         default:
  47.         cur_flag = def_flag;
  48.         }
  49.       } else {
  50.      ++head;
  51.      if ((input = fopen (argv[i], "r")) == NULL) {
  52.         fputs ("wc: can't open ", stderr);
  53.         fputs (argv[i], stderr);
  54.         putc ('\n', stderr);
  55.         continue;
  56.      } else {
  57.         wc (input, argv[i]);
  58.         fclose (input);
  59.      }
  60.       }
  61.    }
  62.    if (!head)
  63.       wc (stdin, "");
  64.    else if ( head > 1)
  65.       print (tlines, twords, tchars, "total");
  66. }
  67.  
  68.  
  69. /* count words, etc. in 1 file
  70.  * inspired by example from: K.&R. P. 18    */
  71.  
  72. wc (fp, fn)
  73. FILE *fp;
  74. char *fn;
  75. {
  76.    register int in, c;
  77.  
  78.    in = FALSE;
  79.    lines = words = chars = 0;
  80.    while ((c = getc (fp)) != EOF) {
  81.       ++chars;
  82.       if (c == '\n') {
  83.      ++lines;
  84.      in = FALSE;
  85.       } else if (c == ' ' || c == '\t')
  86.      in = FALSE;
  87.       else if (!in) {
  88.      in = TRUE;
  89.      ++words;
  90.       }
  91.    }
  92.    print (lines, words, chars, fn);
  93.    tlines += lines;
  94.    twords += words;
  95.    tchars += chars;
  96. }
  97.  
  98.  
  99. /* print - print counts for a file */
  100.  
  101. print (lines, words, chars, fn)
  102. long lines, words, chars;
  103. char *fn;
  104. {
  105.    if (cur_flag & COUNT_LINES) {
  106.       convert (lines, string);
  107.       fputs (string, stdout);
  108.    }
  109.    if (cur_flag & COUNT_WORDS) {
  110.       convert (words, string);
  111.       fputs (string, stdout);
  112.    }
  113.    if (cur_flag & COUNT_CHARS) {
  114.       convert (chars, string);
  115.       fputs (string, stdout);
  116.    }
  117.    if (head)
  118.       fputs (fn, stdout);
  119.    putc ('\n', stdout);
  120. }
  121.  
  122.  
  123. convert (val, string)
  124. register long val;
  125. register char *string;
  126. {
  127.    register int i = 7;
  128.  
  129.    if (val == 0)
  130.       string[i--] = '0';
  131.    while (val != 0) {
  132.       register long j, k;
  133.       j = val / 10;
  134.       k = val - 10 * j;
  135.       val = j;
  136.       string[i--] = '0' + k;
  137.       if (i < 0)
  138.      break;
  139.    }
  140.    while (i >= 0)
  141.       string[i--] = ' ';
  142. }
  143.  
  144.  
  145. _wb_parse ()
  146. {
  147. }
  148.